package com.testmiuu.club.hibernate;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author yuzhong 2019/4/23
 */

public class Test {

    public static void main(String[] args) {
        Filter f1 = new Filter();
        f1.setId(10);  //查询id为10的用户
        f1.setEmail("hha@qq.com");

        Filter f2 = new Filter();
        f2.setUserName("lucy"); //模糊查询用户名为lucy的用户
        Filter f3 = new Filter();
        f3.setEmail("2211735722@qq.com,yuzhong@163.com,yuzhong@sina.com"); //查询邮箱为其中任意一个的用户

        String sq1 = query(f1);
        String sq2 = query(f2);
        String sq3 = query(f3);
        System.out.println(sq1);
        System.out.println(sq2);
        System.out.println(sq3);

        Student s1 = new Student();
        s1.setId(1);
        s1.setUserName("快乐");
        s1.setAddress("上海");
        System.out.println(query(s1));
    }

    private static String query(Object f) {
        StringBuilder sb = new StringBuilder();
        //1.获取到class
        Class c = f.getClass();
        //2.获取到table的名字
        boolean exists = c.isAnnotationPresent(Table.class);
        if (!exists) {
            return null;
        }
        Table t = (Table) c.getAnnotation(Table.class);
        String tableName = t.value();
        sb.append("select * from ").append(tableName).append(" where 1=1");
        //3.遍历所有的字段
        Field[] fArray = c.getDeclaredFields();
        for (Field field : fArray) {
            //4.处理每个字段对应的SQL
            //4.1 拿到字段名
            boolean fExists = field.isAnnotationPresent(Column.class);
            if (!fExists) {
                continue;
            }
            Column annotation = field.getAnnotation(Column.class);
            String columnName = annotation.value();
            //4.2 拿到字段值
            //取字段的名称
            String fieldName = field.getName();
            //获取get方法的名字,通过反射调用这个方法,拿到对应的值  get+字段名 首字母大写
            String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
            Object fieldValue = null;
            try {
                Method getMethod = c.getMethod(getMethodName);
                fieldValue = getMethod.invoke(f);
            } catch (Exception e) {
                e.printStackTrace();
            }

            //4.3 拼装SQL
            if (fieldValue == null || (fieldValue instanceof Integer && (Integer) fieldValue == 0)) {
                continue;
            }
            sb.append(" and ").append(columnName);

            //判断字符串要加单引号
            if (fieldValue instanceof String) {
                if (((String) fieldValue).contains(",")) {
                    String[] values = ((String) fieldValue).split(",");
                    sb.append(" in(");
                    for (String v : values) {
                        sb.append("'").append(v).append("'").append(",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                    sb.append(")");
                } else {
                    sb.append("=").append("'").append(fieldValue).append("'");
                }
            } else if (fieldValue instanceof Integer) {
                sb.append("=").append(fieldValue);
            }

        }
        return sb.toString();
    }

}
